import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Test {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);
        System.out.println("===============");

        for (int i = 0; i < list.size(); i++) {
            Integer a = list.get(i);
            System.out.print(a + " ");
        }
        System.out.println();
        System.out.println("================");

        for (int x : list
             ) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("=======迭代器========");

        Iterator<Integer> it = list.iterator();
        // 判断有没有下一个
        while (it.hasNext()){
            // 打印下一个 然后走到打印的值的地方
            System.out.print(it.next() + " ");
        }
        System.out.println();

        System.out.println("=========迭代器2=========");

        // 默认是从0下标开始打印
        // ListIterator<Integer> it2 = list.listIterator();
        // 从指定位置开始打印
        ListIterator<Integer> it2 = list.listIterator(2);
        while (it2.hasNext()){
            System.out.print(it2.next() + " ");
        }
        System.out.println();

        System.out.println("========迭代器3========");
        ListIterator<Integer> it3 = list.listIterator(list.size());
        while (it3.hasPrevious()){
            System.out.print(it3.previous() + " ");
        }
        System.out.println();
    }

    public static void main3(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        // list.add(0,99);
        System.out.println(list);

        System.out.println("===============");
        // [1，3) 注意: subList() 这里的截取实际上是返回原数组所截取的初始地址
        List<Integer> subList = list.subList(1,3);
        subList.set(0,99);
        System.out.println(subList);

        System.out.println(list);

        Integer ii = 3;
        // int a = 2;
        list.remove(ii);
        System.out.println(list);
    }

    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);

        ArrayList<Integer> list2 = new ArrayList<>(13);
        list2.add(11);
        list2.add(21);
        list2.add(31);
        System.out.println(list2);

        // ArrayList<String> list4 = new ArrayList<>();
        // 用一个集合类构造ArrayList, 构造完成之后和list2的元素一样
        ArrayList<Integer> list3 = new ArrayList<>(list2);
        System.out.println(list3);
    }

    public static void main1(String[] args) {
        MyArrayList arrayList = new MyArrayList(5);
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(4, 6);
        arrayList.dispaly();
        arrayList.remove(5);
        arrayList.dispaly();
    }
}
